<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #663399;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            width: 350px;
        }
        fieldset {
            border: 2px solid #663399;
            border-radius: 5px;
            padding: 20px;
        }
        legend {
            color: #663399;
            font-size: 1.2em;
            font-weight: bold;
            padding: 0 10px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #663399;
            font-weight: bold;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #663399;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            width: 100%;
        }
        button:hover {
            background-color: #5a2d8a;
        }
        .error {
            color: red;
            font-size: 0.9em;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <fieldset>
            <legend>用户登录</legend>
            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" name="username">
                    <div id="usernameError" class="error">用户名不能为空且长度应为8-20个字符</div>
                </div>
                <div class="form-group">
                    <label for="password">密码:</label>
                    <input type="password" id="password" name="password">
                    <div id="passwordError" class="error">密码不能为空且长度应为8-20个字符</div>
                </div>
                <button type="submit">登录</button>
            </form>
        </fieldset>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单默认提交行为

            // 获取输入值
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();

            // 获取错误提示元素
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');

            // 重置错误状态
            usernameError.style.display = 'none';
            passwordError.style.display = 'none';

            let isValid = true;

            // 验证用户名
            if (username === '') {
                usernameError.textContent = '用户名不能为空';
                usernameError.style.display = 'block';
                document.getElementById('username').focus();
                isValid = false;
            } else if (username.length < 8 || username.length > 20) {
                usernameError.textContent = '用户名长度应为8-20个字符';
                usernameError.style.display = 'block';
                document.getElementById('username').focus();
                isValid = false;
            }

            // 验证密码
            if (password === '') {
                passwordError.textContent = '密码不能为空';
                passwordError.style.display = 'block';
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            } else if (password.length < 8 || password.length > 20) {
                passwordError.textContent = '密码长度应为8-20个字符';
                passwordError.style.display = 'block';
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            }

            // 如果验证通过
            if (isValid) {
                alert('登录成功！');
                // 这里可以添加实际的登录逻辑
                // this.submit();
            }
        });
    </script>
</body>
</html>